import del from './atom/db/del';
import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	rowdata: Array<{ _id: string, staff_number: string }>
}

async function atom(msg: Message) {
	const systemid = '6051';
	const { exec, prepare } = del(action_id, session_id, spaceid, systemid);
	prepare('teach_staff', msg.rowdata);// del 教职工

	const { exec: e1, prepare: p1 } = query(action_id, session_id, spaceid, systemid);
	msg.rowdata.forEach((s: { _id: string, staff_number: string }) => {
		p1('position_contact', ['_id'], { staff_number: s.staff_number });//query 职位及联系方式
		p1('work_experience', ['_id'], { staff_number: s.staff_number });//query 工作经历
		p1('edu_experience', ['_id'], { staff_number: s.staff_number });//query 教育经历
	});
	const [position_contacts, work_experiences, edu_experiences] = await e1();
	position_contacts.forEach((d: { _id: string }) => {
		prepare('position_contact', [{ _id: d._id }]);//del 职位及联系方式
	});
	work_experiences.forEach((d: { _id: string }) => {
		prepare('work_experience', [{ _id: d._id }]);//del 工作经历
	});
	edu_experiences.forEach((d: { _id: string }) => {
		prepare('edu_experience', [{ _id: d._id }]);//del 教育经历
	});
	const ret = await exec()
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
